﻿// FaceSDK_CSharp.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "VYAlgoSDKC.h"
#include <fstream>
#include <sstream>
__declspec(dllexport) bool sdk_init_ok = false;
VYAlgoHandle vyhandle;
using namespace std;

__declspec(dllexport) int face_sdk_init(const char* datafile)
{
	if (!sdk_init_ok)
	{
		sdk_init_ok = true;
		vyhandle = _VYALGOSDKC_init(datafile);
	}
	
	return 0;
}
__declspec(dllexport) int face_sdk_extract(const char* imgfile,char* featcode)
{
	if (NULL == vyhandle)
		return -1;
	int feature_code_num = 640;
	VYFACELAND *facesArray = NULL;

	VYFeature *pFeature_list = NULL;
	//VYFeature feature;
	int facesnum = 0;
	/*ofstream outputfile;
	outputfile.open("D:\\failure.txt", ios::out);
	outputfile << "start";*/
	VYAlgo_result_t re = _VYALGOSDKC_extract(vyhandle, imgfile, &facesnum, &facesArray, &pFeature_list, FLAG_EXTRACT_WITH_DETECTION);
	if (ALGO_OK != re)
		return -2;
	if (facesnum < 1)
	{

		return -3;

	}
	else
	{
		int big_index = 0;
		float big_area = 0;
		for (size_t m = 0; m < facesnum; m++)
		{
			float facearea = facesArray[m].face._rc._height*facesArray[m].face._rc._width;
			if (facearea > big_area)
			{
				big_area = facearea;
				big_index = m;
			}

		}
		
		//feature._nFeatureSize = pFeature_list[big_index]._nFeatureSize;
	
		/*outputfile << "feature code OK!";
		outputfile.flush();
		outputfile.close();*/
		memcpy(featcode, pFeature_list[big_index]._pFeatureData, feature_code_num);
		
		_VYALGOSDKC_release_detect_result(&facesnum, &facesArray);
		_VYALGOSDKC_release_extract_result(&facesnum, &pFeature_list);



	}

	return 0;
}
__declspec(dllexport) float face_sdk_compare(char* f1, char* f2,int codelen)
{
	VYFeature fc1, fc2;
	fc1._nFeatureSize = codelen;
	fc2._nFeatureSize = codelen;
	fc1._pFeatureData = f1;
	fc2._pFeatureData = f2;
	float  score = -1.0;
	score = _VYALGOSDKC_compare(vyhandle, &fc1, &fc2, 1.0);
	return score;	
}